﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace EncryptionServer.CrcCheck
{
    public partial class FrmCRCCheck : Form
    {
        //数据库访问类
        DataAccess.DalData dal = new CrcCheck.DataAccess.DalData();
        //bool
        bool CanExit = false;
        //crc32集合
        public static List<int> ListCrc = new List<int>();
        //Tcp监听线程
        Thread thTcp = null;
        //委托
        public delegate void DelErrorConsole(string msg);           //异常
        public DelErrorConsole InvokeErrorConsole;
        public delegate void DelShowCrcMsg(string msg);             //Crc校验结果
        public DelShowCrcMsg InvokeShowCrcMsg;
        public FrmCRCCheck()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            try
            {
                
                //委托
                InvokeErrorConsole = new DelErrorConsole(ErrorConsole);     //异常
                InvokeShowCrcMsg = new DelShowCrcMsg(ShowCrcMsg);           //校验结果
                //删除最近2天以外的所有CRC码，并取得最近2天所有的CRC码
                dal.DelCrc();
                ListCrc = dal.GetCrc();
                //独立的线程专门监听TCP消息
                thTcp = new Thread(StartTcpListen);
                thTcp.IsBackground = true;
                thTcp.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确定", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        /// <summary>
        /// Tcp监听
        /// </summary>
        private void StartTcpListen()
        {
            try
            {
                TcpListener tl = new TcpListener(IPAddress.Any,Properties.Settings.Default.OrderCRCServerPort);
                tl.Start();
                while (true)                    //不停地监听是否有新连接
                {
                    Socket S = tl.AcceptSocket();
                    CrcThread.ThreadControl tc = new CrcCheck.CrcThread.ThreadControl();
                    tc.S = S;
                    tc.Dal = dal;
                    tc.frm = this;
                    tc.StartThread();

                    //CrcThread.ThreadCrc crc = new CrcCheck.CrcThread.ThreadCrc();
                    //crc.Dal = dal;
                    //crc.frm = this;
                    //crc.S = S;
                }
            }
            catch
            {
                MessageBox.Show("验证照片重复服务程序异常，请重新启动", "确定", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Application.Exit();             //退出程序
            }
        }
        /// <summary>
        /// 线程出错处理
        /// </summary>
        /// <param name="msg"></param>
        public void ErrorConsole(string msg)
        {
            MessageBox.Show(msg, "确定", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            Application.Exit();                 //退出程序
        }

        public void ShowCrcMsg(string msg)
        {
            lbMsg.Items.Add(msg);
        }

        //恢复为原始大小
        private void tsRetrive_Click(object sender, EventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
            this.TopMost = true;
            this.Show();
            this.TopMost = false;
        }

        //双击恢复
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
            this.TopMost = true;
            this.Show();
            this.TopMost = false;
        }

        //最小化到系统托盘
        private void tsMin_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            this.WindowState = FormWindowState.Minimized;
            this.Hide();
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (CanExit == false)           
            {
                this.Visible = false;
                this.WindowState = FormWindowState.Minimized;
                this.Hide();
                e.Cancel = true;
            }
        }

        //退出服务
        private void tsExit_Click(object sender, EventArgs e)
        {
            CanExit = true;
            this.Close();
        }

        /*
        //配置基础信息
        private void tsConfig_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                FrmConfig frm = FrmConfig.GetInstance;
                frm.Show();
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确定", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }*/

    }
}
